package leet.ku._202;

public class Solution {
	
	public boolean isHappy(int n) {
        int x = n;
        int y = n;
        while (true) {
        	if (x == 1)
        		return true;
        	x = cal(x);
        	if (y == 1)
        		return true;
        	y = cal(y);
        	if (y == 1)
        		return true;
        	y = cal(y);
        	if (x == y)
        		break;
        }
        return false;
    }
	
	private int cal(int x) {
		int ans = 0;
		while (x > 0) {
			ans += (x % 10) * (x % 10);
			x /= 10;
		}
		return ans;
	}
}
